import java.net.*;
import java.util.ArrayList;
import java.io.IOException;

public class ABCServer
{
	ServerSocket serverSocket;
	boolean runflag;
	//ArrayList<Match> maches;

	public ABCServer(int a_port) throws IOException
	{
		serverSocket = new ServerSocket(a_port);
		runflag = true;
	}

	public void listen() throws IOException
	{
		Socket tmp;

		while(runflag)
		{
			System.out.println("Starting new match...");
			Match match = new Match();

			System.out.println("Waiting for players...");

			tmp = serverSocket.accept();
			System.out.println("Got player 1 for new match");
			match.addPlayer(tmp);

			tmp = serverSocket.accept();
			System.out.println("Got player 2 for new match");
			match.addPlayer(tmp);
		}
	}

	public static void main(String[] args) throws IOException
	{
		try{
			ABCServer server;
			if( args.length < 1 )
				server = new ABCServer(6667);
			else
				server = new ABCServer(Integer.parseInt(args[0]));
			server.listen();
		}
		catch(Exception e){};
	}
}